home *** CD-ROM | disk | FTP | other *** search
- This file is trap.def, from which is created trap.c.
- It implements the builtin "trap" in Bash.
-
- Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
-
- This file is part of GNU Bash, the Bourne Again SHell.
-
- Bash is free software; you can redistribute it and/or modify it under
- the terms of the GNU General Public License as published by the Free
- Software Foundation; either version 1, or (at your option) any later
- version.
-
- Bash is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or
- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- for more details.
-
- You should have received a copy of the GNU General Public License along
- with Bash; see the file COPYING. If not, write to the Free Software
- Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
-
- $PRODUCES trap.c
-
- $BUILTIN trap
- $FUNCTION trap_builtin
- $SHORT_DOC trap [arg] [signal_spec]
- The command ARG is to be read and executed when the shell receives
- signal(s) SIGNAL_SPEC. If ARG is absent all specified signals are
- reset to their original values. If ARG is the null string this
- signal is ignored by the shell and by the commands it invokes. If
- SIGNAL_SPEC is EXIT (0) the command ARG is executed on exit from
- the shell. The trap command with no arguments prints the list of
- commands associated with each signal number. SIGNAL_SPEC is either
- a signal name in <signal.h>, or a signal number. The syntax `trap -l'
- prints a list of signal names and their corresponding numbers.
- Note that a signal can be sent to the shell with "kill -signal $$".
- $END
-
- #include <sys/types.h>
- #include <signal.h>
- #include "../shell.h"
- #include "../trap.h"
-
- /* The trap command:
-
- trap <arg> <signal ...>
- trap <signal ...>
- trap -l
- trap [--]
-
- Set things up so that ARG is executed when SIGNAL(s) N is recieved.
- If ARG is the empty string, then ignore the SIGNAL(s). If there is
- no ARG, then set the trap for SIGNAL(s) to its original value. Just
- plain "trap" means to print out the list of commands associated with
- each signal number. Single arg of "-l" means list the signal names. */
-
- /* Possible operations to perform on the list of signals.*/
- #define SET 0 /* Set this signal to first_arg. */
- #define REVERT 1 /* Revert to this signals original value. */
- #define IGNORE 2 /* Ignore this signal. */
-
- trap_builtin (list)
- WORD_LIST *list;
- {
- register int i;
- int list_signal_names = 0;
-
- while (list)
- {
- if (strcmp (list->word->word, "-l") == 0)
- {
- list_signal_names++;
- list = list->next;
- }
- else if (strcmp (list->word->word, "--") == 0)
- {
- list = list->next;
- break;
- }
- else if ((*list->word->word == '-') && (list->word->word[1] != '\0'))
- {
- bad_option (list->word->word);
- return (EXECUTION_FAILURE);
- }
- else
- break;
- }
-
- if (!list && !list_signal_names)
- {
- for (i = 0; i < NSIG; i++)
- if (trap_list[i] != (char *)DEFAULT_SIG)
- printf ("trap -- '%s' %s\n",
- (trap_list[i] == (char *)IGNORE_SIG) ? "" : trap_list[i],
- signal_name (i));
- return (EXECUTION_SUCCESS);
- }
-
- if (list_signal_names)
- {
- int column = 0;
-
- for (i = 0; i < NSIG; i++)
- {
- printf ("%2d) %s", i, signal_name (i));
- if (++column < 4)
- printf ("\t");
- else
- {
- printf ("\n");
- column = 0;
- }
- }
- if (column != 0)
- printf ("\n");
- return (EXECUTION_SUCCESS);
- }
-
- if (list)
- {
- char *first_arg = list->word->word;
- int operation = SET, any_failed = 0;
-
- if (signal_object_p (first_arg))
- operation = REVERT;
- else
- {
- list = list->next;
- if (*first_arg == '\0')
- operation = IGNORE;
- else if (strcmp (first_arg, "-") == 0)
- operation = REVERT;
- }
-
- while (list)
- {
- extern int interactive;
- int sig;
-
- sig = decode_signal (list->word->word);
-
- if (sig == NO_SIG)
- {
- builtin_error ("%s: not a signal specification",
- list->word->word);
- any_failed++;
- }
- else
- {
- switch (operation)
- {
- case SET:
- set_signal (sig, first_arg);
- break;
-
- case REVERT:
- restore_default_signal (sig);
-
- /* Always ignore SIGQUIT. */
- if (sig == SIGQUIT)
- signal (SIGQUIT, SIG_IGN);
-
- /* Ignore some other signals if we are interactive. */
- if (interactive)
- {
- if (sig == SIGTERM)
- signal (SIGTERM, SIG_IGN);
-
- #if defined (JOB_CONTROL)
- if (sig == SIGTTIN ||
- sig == SIGTTOU ||
- sig == SIGTSTP)
- signal (sig, SIG_IGN);
- #endif /* JOB_CONTROL */
- }
- break;
-
- case IGNORE:
- ignore_signal (sig);
- break;
- }
- }
- list = list->next;
- }
- return ((!any_failed) ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
- }
- }
-